package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.CheckImageBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.CheckImageMapper;
import com.molichuxing.services.infrastructure.dto.request.modify.CheckImageModifyDto;
import com.molichuxing.services.infrastructure.dto.response.CheckImageDto;
import com.molichuxing.services.infrastructure.service.CheckImageService;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 车辆图片表 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-15
 */
@Service("checkImageService")
public class CheckImageServiceImpl implements CheckImageService {

  private static final Logger log = Logger.getLogger(CheckImageServiceImpl.class);

  @Resource
  private CheckImageMapper checkImageMapper;


  @Override
  public List<CheckImageDto> getCheckImageByCheckId(Integer checkId) {
    return Convert.toCheckImageDtoList(checkImageMapper.getByCheckId(checkId));
  }

  @Override
  public CheckImageDto getCheckPdfByCheckId(Integer checkId) {
    return Convert.toCheckImageDto(checkImageMapper.getCheckPdfByCheckId(checkId));
  }

  @Override
  @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
  public int  createCheckImage(Integer checkId, List<CheckImageModifyDto> checkImageModifyDto) throws Exception {
      if(checkImageModifyDto !=null && checkImageModifyDto.size()>0){
        for (CheckImageModifyDto imageModifyDto : checkImageModifyDto) {
          CheckImageBean checkImageBean   = Convert.toCheckImageBean(checkId,imageModifyDto);
          int num  = checkImageMapper.insertCheckImageBean(checkImageBean);
          if(num < 0){
            throw new Exception("新增车辆检验图片信息失败");
          }
        }
      }
    return 1;
  }

  @Override
  public int createPdiCheckImage(Integer checkId, String domainPath,Integer type ) {
    CheckImageBean checkImageBean  = new  CheckImageBean();
    checkImageBean.setCheckId(checkId);
    checkImageBean.setDomainPath(domainPath);
    checkImageBean.setType(type);
    return  checkImageMapper.insertCheckImageBean(checkImageBean);
  }
}
